﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.IO;
using Aspose.Words;
using Aspose;
using Aspose.Words.Drawing;
using Aspose.Words.Reporting;
using Aspose.Words.Viewer;
using NickLee.Common;

namespace GPCSC.Web.sjs
{
    public partial class sq : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

                fbuild();

        }
        protected void wts()
        {
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", Usual.f_querystringToString("lgid"));
            DataSet dwts = new SqlReadXml().ReaderDataSet("countWT", htable);
            this.MtxtD_SQ_JJWTQK_XYJJWTHLJS.Value = dwts.Tables[0].Rows[0]["wts"].ToString();
            DataSet jdwts = new SqlReadXml().ReaderDataSet("countjjWT", htable);
            this.MtxtD_SQ_JJWTQK_YJJJS.Value = jdwts.Tables[0].Rows[0]["jwts"].ToString();
        }
        protected void fbuild()
        {

            DataSet ds = new DataSet();
            Hashtable htable = new Hashtable();
            htable.Add("@id", Usual.f_querystringToString("hdid"));
            ds = new SqlReadXml().ReaderDataSet("selsq", htable);
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        this.Button3.Visible = false;
                        this.types.Value = "1";
                        UIFactory.UIFactory_Set(this, ds.Tables[0]);
                    }
                }
            }
                this.MtxtD_SQID.Value = Usual.f_querystringToString("hdid");
                this.MtxtD_SQHDID.Value = Usual.f_querystringToString("hdid");
                this.MtxtD_SQLGID.Value = Usual.f_querystringToString("lgid");
                byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
                Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
                Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
                MtxtD_SQ_XM.Value = users.USERNAME.ToString();
                MtxtD_year.Value = DateTime.Now.Year.ToString();
                MtxtD_month.Value = DateTime.Now.Month.ToString();
                MtxtD_day.Value = DateTime.Now.Day.ToString();
        }
        protected void savenc()
        {
            try
            {
                Hashtable htable = f_getElements();
                byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
                Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
                Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
                htable.Add("usernames", users.REALNAME.ToString());
                htable.Add("postids", users.CL_POSTID_PK.ToString());
                htable.Add("postnames", users.POSTName.ToString());
                SQLServerDAL.databyhtable dtable = new SQLServerDAL.databyhtable();
                dtable.databyhtables(htable, "add", "SheQu", "", "");
                addCout();
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }

        public Hashtable f_getElements()
        {
            //UIFactory Get Values as Hashtable:界面工厂获取值，并保存在哈希表
            base.o_uiTable.Clear();
            base.o_uiTable = UIFactory.UIFactory_Get(this);
            return base.o_uiTable;
        }

        protected void cterWord1(DataSet ds)
        {
            try
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    Stream f = new FileStream(Server.MapPath("~") + @"\grid\sqhdxs.doc", FileMode.Open);
                    Document doc = new Document(f);
                    doc.MailMerge.Execute(ds.Tables[0]);
                    doc.MailMerge.DeleteFields();
                    doc.Save(Server.UrlEncode("下基层“三进三帮”活动情况反馈表(社区).doc"), SaveFormat.Doc, Aspose.Words.SaveType.OpenInWord, Response);


                }
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }
        private void KillProcess(string processName)
        {
            System.Diagnostics.Process myproc = new System.Diagnostics.Process();
            //得到所有打开的进程 
            try
            {
                foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcessesByName(processName))
                {
                    if (!thisproc.CloseMainWindow())
                    {
                        thisproc.Kill();
                    }
                }
            }
            catch (Exception Exc)
            {
                throw Exc;
            }
        }

        //提交活动形式
        protected void Button3_Click(object sender, EventArgs e)
        {

            savenc();
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>locking();</script>");
        }

        //下载活动形式表格
        protected void Button4_Click(object sender, EventArgs e)
        {
           // KillProcess("WINWORD");
            try
            {
                DataSet ds = new DataSet();
                Hashtable htable = new Hashtable();
                htable = UIFactory.UIFactory_Get(this);
                DataTable tables = HashtableAndDatatable.Retable(htable);
                ds.Tables.Add(tables);
                cterWord1(ds);
            }
            catch
            {
            }
            finally
            {
                KillProcess("WINWORD");
            }
        }
        protected void addCout()
        {
            Hashtable htab = new Hashtable();
            SqlReadXml SR = new SqlReadXml();
            #region
            string kns = this.kns.Value;
            if (kns != "")
            {
                string[] ids = kns.Split('#');
                if (ids.Length > 0)
                {
                    for (int i = 0; i < ids.Length - 1; i++)
                    {
                        htab.Clear();
                        htab.Add("@id", ids[i]);
                        SR.NotDoSQL("update_GGH_table_byid", htab);
                    }
                }
            }
            string zfs = ggs.Value;
            if (zfs != "")
            {
                string[] ids = zfs.Split('#');
                if (ids.Length > 0)
                {
                    for (int i = 0; i < ids.Length - 1; i++)
                    {
                        htab.Clear();
                        htab.Add("@id", ids[i]);
                        SR.NotDoSQL("update_GGH_zfs_byid", htab);
                    }
                }
            }
            #endregion

        }
    }
}